/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entidades;

import java.io.Serializable;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import jsf.util.SHA1;

/**
 *
 * @author dvasquez
 */
@Entity
@Table(name = "usuario")
@NamedQueries({@NamedQuery(name = "Usuario.findAll", query = "SELECT u FROM Usuario u"), @NamedQuery(name = "Usuario.findByRut", query = "SELECT u FROM Usuario u WHERE u.rut = :rut"), @NamedQuery(name = "Usuario.findByDigito", query = "SELECT u FROM Usuario u WHERE u.digito = :digito"), @NamedQuery(name = "Usuario.findByPrimerNombre", query = "SELECT u FROM Usuario u WHERE u.primerNombre = :primerNombre"), @NamedQuery(name = "Usuario.findBySegundoNombre", query = "SELECT u FROM Usuario u WHERE u.segundoNombre = :segundoNombre"), @NamedQuery(name = "Usuario.findByApellidoPaterno", query = "SELECT u FROM Usuario u WHERE u.apellidoPaterno = :apellidoPaterno"), @NamedQuery(name = "Usuario.findByApellidoMaterno", query = "SELECT u FROM Usuario u WHERE u.apellidoMaterno = :apellidoMaterno"), @NamedQuery(name = "Usuario.findByClave", query = "SELECT u FROM Usuario u WHERE u.clave = :clave")})
public class Usuario implements Serializable,Comparable<Usuario> {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "rut")
    private String rut;
    @Column(name = "digito")
    private Character digito;
    @Column(name = "primerNombre")
    private String primerNombre;
    @Column(name = "segundoNombre")
    private String segundoNombre;
    @Column(name = "apellidoPaterno")
    private String apellidoPaterno;
    @Column(name = "apellidoMaterno")
    private String apellidoMaterno;
    @Column(name = "clave")
    private String clave;
    @JoinTable(name = "usuarioRol", joinColumns = {@JoinColumn(name = "rut", referencedColumnName = "rut")}, inverseJoinColumns = {@JoinColumn(name="nombre", referencedColumnName = "nombre")})
    @ManyToMany
    private List<Rol> rolCollection;
    @OneToMany(mappedBy = "rut")
    private List<Formulario> formularioCollection;

    public Usuario() {
    }

    public Usuario(String rut) {
        this.rut = rut;
    }

    public List<Formulario> getFormularioCollection() {
        return formularioCollection;
    }

    public void setFormularioCollection(List<Formulario> formularioCollection) {
        this.formularioCollection = formularioCollection;
    }

    public List<Rol> getRolCollection() {
        return rolCollection;
    }

    public void setRolCollection(List<Rol> rolCollection) {
        this.rolCollection = rolCollection;
    }

    public String getRut() {
        return rut;
    }

    public void setRut(String rut) {
        this.rut = rut;
    }

    public Character getDigito() {
        return digito;
    }

    public void setDigito(Character digito) {
        this.digito = digito;
    }

    public String getPrimerNombre() {
        return primerNombre;
    }

    public void setPrimerNombre(String primerNombre) {
        this.primerNombre = primerNombre;
    }

    public String getSegundoNombre() {
        return segundoNombre;
    }

    public void setSegundoNombre(String segundoNombre) {
        this.segundoNombre = segundoNombre;
    }

    public String getApellidoPaterno() {
        return apellidoPaterno;
    }

    public void setApellidoPaterno(String apellidoPaterno) {
        this.apellidoPaterno = apellidoPaterno;
    }

    public String getApellidoMaterno() {
        return apellidoMaterno;
    }

    public void setApellidoMaterno(String apellidoMaterno) {
        this.apellidoMaterno = apellidoMaterno;
    }

    public String getClave(){
        return this.clave;
    }

    public void setClave(String clave){
        this.clave = clave;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (rut != null ? rut.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Usuario)) {
            return false;
        }
        Usuario other = (Usuario) object;
        if ((this.rut == null && other.rut != null) || (this.rut != null && !this.rut.equals(other.rut))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String user = rut + "-" + digito;
        return user;
    }

    public int compareTo(Usuario o) {
        if(this.apellidoPaterno.toLowerCase().compareTo(o.apellidoPaterno.toLowerCase()) == 0){
            if(this.apellidoMaterno.toLowerCase().compareTo(o.apellidoMaterno.toLowerCase()) == 0)
                return this.primerNombre.toLowerCase().compareTo(o.primerNombre.toLowerCase());
            else
                return this.apellidoMaterno.toLowerCase().compareTo(o.apellidoMaterno.toLowerCase());
        }else
            return this.apellidoPaterno.toLowerCase().compareTo(o.apellidoPaterno.toLowerCase());
    }

}
